//
//  KXAccountViewController.swift
//  KXBook
//
//  Created by  on 2022/9/10.
//

import UIKit

class KXAccountViewController: KXViewController {
    @IBOutlet var tableView: UITableView!
    @IBOutlet var bgTableView: UIView!
    @IBOutlet var bgTableViewH: NSLayoutConstraint!
    var array = [["img":"组 73","title":"注销账号"]]
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = LS("账号管理")
        self.bgTableViewH.constant = CGFloat(80 * self.array.count)
        self.tableView.register(KXMeCell.nib(), forCellReuseIdentifier: KXMeCell.ID())
        self.bgTableView.layer.masksToBounds = false
        self.bgTableView.layer.shadowColor = "#9B9B9B".color().cgColor
        self.bgTableView.layer.shadowOffset = CGSize(width:0, height:0)
        self.bgTableView.layer.shadowRadius = 10
        self.bgTableView.layer.shadowOpacity = 1
        self.bgTableView.layer.cornerRadius = 10
    }
    
    @IBAction func acionForBtn(_ sender: UIButton) {
        self.alert(LS("提示"), LS("确定退出吗？")) {
        } _: {
            self.updateData()
        }
    }
    func updateData(){
        APP.user = User()
        User.bg_clear(nil)
        APP.defaulType(.pay)
        APP.defaulType(.In)
        NotifyTool.post(NotifyName.updateUser)
        self.backBtnAction()
    }
}


//MARK: - UITableViewDataSource
extension KXAccountViewController : UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.array.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let dict = self.array[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: KXMeCell.ID()) as! KXMeCell
        cell.img.image = UIImage(named: dict["img"]!)
        cell.title.text = LS(dict["title"]!)
        return cell
    }
}
//MARK: - UITableViewDelegate
extension KXAccountViewController : UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let dict = self.array[indexPath.row]
        let title = dict["title"]
        switch (title){
        case ("注销账号"):
            self.alert(LS("提示"), LS("注销账号后，账号将不存在，是否继续？")) {
            } _: {
                network.get(KXConst.api_delUser, para: nil, header: nil) { response in
                    KXTypeModel.bg_clear(nil)
                    KXRecord.bg_clear(nil)
                    self.updateData()
                } fail: { error in }
            }
        default:
            break
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
}
